//判断用户是否登录
$(function () {
    $.ajax({
        type: "post",
        url: "http://localhost:8080/user/getCurrentUser",
        success: function (result) {
            if (result.code == '200') {
                userInfo();
            } else {
                alert("请先登录账号！");
                window.location.href = "../../templates/login.html";
            }
        }
    });
});

/*
    *根据id查找用户
    */
function findById() {
    let query_userid = document.getElementById("userid").value;
    let data = {"userid": query_userid};
    $.post('http://localhost:8080/user/findById', data, function (result) {
        let res = result.object;
        if (result.code == '200') {
            let str = "";
            str += "<tr><td>\n" +
                "<input type='checkbox' name='checkbox' value='" + res.userid + "'/>\n" +
                "</td>";
            str += "<td>" + res.userid + "</td>";
            str += "<td>" + res.username + "</td>";
            str += "<td>" + res.password + "</td>";
            str += "<td>" + res.sex + "</td>";
            str += "<td>" + res.faculty + "</td>";
            if (res.priority == 2) {
                str += "<td>管理员</td>";
            } else if (res.priority == 1) {
                str += "<td>教师</td>";
            } else if (res.priority == 0) {
                str += "<td>学生</td>";
            } else {
                alert("请求出错");
            }
            str += "<td>\n" +
                "<a title='编辑' onclick=\"user_edit_show('编辑','user-edit.html'," + res.userid + ")\" href='javascript:;'>\n" +
                "<i class='layui-icon'>&#xe642;</i>\n" +
                "</a>\n" +
                "<a title='删除' onclick='user_del(this," + res.userid + ")' href='javascript:;'>\n" +
                "<i class='layui-icon'>&#xe640;</i>\n" +
                "</a>\n" +
                "</td></tr>";
            $("#users").html(str);
        }
    });
}

/*用户-删除*/
function user_del(obj, userid) {
    layer.confirm('确认要删除吗？', function (index) {
        //发异步删除数据
        $.ajax({
            type: "delete",
            url: "http://localhost:8080/user/deleteById",
            data: "userid=" + userid,
            success: function (result) {
                if (result.code = "200") {
                    //obj是属于js对象
                    //js对象转换成jquery对象
                    let tr = $(obj).parent().parent();
                    tr.remove();
                    // $(obj).parents("tr").remove();
                    layer.msg('已删除!', {icon: 1, time: 1000});
                }
            }
        });
    });
}

//删除多个用户
function delSome() {
    let checkboxes = document.getElementsByName("checkbox");
    let check_val = [];
    for (let k in checkboxes) {
        if (checkboxes[k].checked)
            check_val.push(checkboxes[k].value);
    }
    $.ajax({
        type: "delete",
        url: "http://localhost:8080/user/deleteSome",
        data: "ids=" + check_val,
        success: function (result) {
            if (result.code = "200") {
                //obj是属于js对象
                //js对象转换成jquery对象
                let trs = [];
                for (let s = 0; s < checkboxes.length; s++) {
                    if (checkboxes[s].checked) {
                        let tr = checkboxes[s];
                        trs.push(tr);
                    }
                }
                for (let i = 0; i < trs.length; i++) {
                    let tr = $(trs[i]).parent().parent();
                    tr.remove();
                }
            }
        }
    });
}

/*
    参数解释：
    title   标题
    url     请求的url
    id      需要操作的数据id
    w       弹出层宽度（缺省调默认值）
    h       弹出层高度（缺省调默认值）
*/
function user_edit_show(title, url, id, w, h) {
    if (title == null || title == '') {
        title = false;
    }
    if (url == null || url == '') {
        url = "404.html";
    }
    if (id == null || id == '') {
        alert("出错了！")
    } else {
        if (window.localStorage) {
            localStorage.id = id;
        }
    }
    if (!(w == null || w == '')) {
    } else {
        w = ($(window).width() * 0.9);
    }
    if (h == null || h == '') {
        h = ($(window).height() - 50);
    }

    layer.open({
        type: 2,
        area: [w + 'px', h + 'px'],
        fix: false, //不固定
        maxmin: true,
        shadeClose: true,
        shade: 0.4,
        title: title,
        content: url
    });
}

/*
    参数解释：
    title   标题
    url     请求的url
    id      需要操作的数据id
    w       弹出层宽度（缺省调默认值）
    h       弹出层高度（缺省调默认值）
*/
function lab_book(title, url, id, w, h) {
    if (title == null || title == '') {
        title = false;
    }
    if (url == null || url == '') {
        url = "404.html";
    }
    if (id == null || id == '') {
        alert("出错了！")
    } else {
        if (window.localStorage) {
            localStorage.id = id;
        }
    }
    if (!(w == null || w == '')) {
    } else {
        w = ($(window).width() * 0.9);
    }
    if (h == null || h == '') {
        h = ($(window).height() - 50);
    }

    layer.open({
        type: 2,
        area: [w + 'px', h + 'px'],
        fix: false, //不固定
        maxmin: true,
        shadeClose: true,
        shade: 0.4,
        title: title,
        content: url
    });
}

//全选
function checkAll(obj) {
    if (obj.checked == true) {
        checkboxed("checkbox")
    } else {
        // alert(obj.checked)
        uncheckboxed("checkbox")

    }

}

function checkboxed(objName) {
    var objNameList = document.getElementsByName(objName);

    if (null != objNameList) {
        // alert("全选操作");
        for (var i = 0; i < objNameList.length; i++) {
            objNameList[i].checked = "checked";
        }
    }
}

function uncheckboxed(objName) {
    var objNameList = document.getElementsByName(objName);

    if (null != objNameList) {
        // alert("取消全选操作");
        for (var i = 0; i < objNameList.length; i++) {
            objNameList[i].checked = "";
        }
    }
}